package com.moorror.cola.strategy.plugin.view.component;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.cola.extension.BizScenario;
import com.alibaba.cola.extension.ExtensionCoordinate;
import com.alibaba.cola.extension.ExtensionPointI;
import com.google.common.collect.Sets;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.project.Project;
import com.moorror.cola.strategy.plugin.bean.BizScenarioExtend;
import com.moorror.cola.strategy.plugin.common.toolbaractions.RefreshRegister;
import com.moorror.cola.strategy.plugin.view.window.CSPluginWindowFactory;
import org.apache.commons.compress.utils.Lists;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author jiyanghuang
 * @Date 2022/3/16 10:34 PM
 */
public class CSActionToolbar {

    private Project project;

    private ActionToolbar toolbar;

    private CSMultiSelect multiSelectProducts;

    private CSMultiSelect multiSelectScenario;

    private CSComboBox bizIdComboBox;

    private CSComboBox useCaseComboBox;

    private CSComboBox scenarioComboBox;


    private DefaultActionGroup rootActionGroup;

    private CSCheckbox openMarker;

    /**
     * 是否展示全部实现
     * 1.true 展示全部实现
     * 2.false 根据策略只显示一个或多个，如果策略不认识则按全部显示
     *
     *     private String bizId = "#defaultBizId#";
     *     private String useCase = "#defaultUseCase#";
     *     private String scenario = "#defaultScenario#";
     */
    private CSCheckbox showAllImpl;

    public CSActionToolbar(Project project){
        this.project = project;
        DefaultActionGroup rootActionGroup = new DefaultActionGroup();
        openMarker = new CSCheckbox("选中按扩展点跳转，不选中按业务身份跳转");
        bizIdComboBox = new CSComboBox(Sets.newHashSet(), "BizId");
//        useCaseComboBox = new CSComboBox(Sets.newHashSet(), "UseCase");
//        scenarioComboBox = new CSComboBox(Sets.newHashSet(), "Scenario");

//        showAllImpl = new CSCheckbox("展示全部扩展点实现，否则按策略显示[动态策略全部展示]");
//        showAllImpl.setCurrentState(false);

        rootActionGroup.add(new RefreshRegister(this));
        rootActionGroup.add(openMarker);
        rootActionGroup.add(bizIdComboBox);
//        rootActionGroup.add(useCaseComboBox);
//        rootActionGroup.add(scenarioComboBox);
//        rootActionGroup.add(showAllImpl);

        toolbar = ActionManager.getInstance().createActionToolbar("toolbar", rootActionGroup, true);
    }

    public ActionToolbar getToolbar(){
        return toolbar;
    }

    /**
     * 返回业务身份、产品、场景
     */
    public void update(){
        Map<ExtensionCoordinate, ExtensionPointI> coordinateExtensionPointIMap = CSPluginWindowFactory.extensionRepository.getExtensionRepo();
        List<String> bizIds = Lists.newArrayList();
        Set<String> useCase = Sets.newHashSet();
        Set<String> scenario = Sets.newHashSet();
        for(Map.Entry<ExtensionCoordinate, ExtensionPointI> entry : coordinateExtensionPointIMap.entrySet()){
            ExtensionCoordinate extensionCoordinate = entry.getKey();
            BizScenario bizScenario = extensionCoordinate.getBizScenario();
            bizIds.add(bizScenario.getUniqueIdentity());
        }
        Collections.sort(bizIds);
        Set<String> bizId = Sets.newLinkedHashSet();
        bizId.addAll(bizIds);
        bizIdComboBox.update(bizId);
    }

    public boolean openMarker(){
        return openMarker.isState();
    }

    public boolean showAllImpl(){
        return showAllImpl.isState();
    }

    public String bizId(){
        return bizIdComboBox.currentItem();
    }

    public String useCase(){
        return useCaseComboBox.currentItem();
    }

    public String scenario(){
        return scenarioComboBox.currentItem();
    }

}
